package longrise.phone.com.bjjt_henan.bindinginformation;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.LFView;
import com.longrise.android.listview_library_refrush.PullToRefreshBase;
import com.longrise.android.listview_library_refrush.PullToRefreshListView;
import com.longrise.bjjt.BaseApplication;

import java.util.ArrayList;

import longrise.phone.com.bjjt_henan.Adapter.MyCollectAdapter;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;
import longrise.phone.com.bjjt_henan.main.CarNewsActivity;

/**
 * Created by Administrator on 2016/8/26.
 */
public class MyCollectView extends LFView implements AdapterView.OnItemClickListener {

    private Context mContext;

    private View mView;

    private PullToRefreshListView mListView;

    private MyCollectAdapter mAdapter;

    private boolean isLoading = false;// 正在加载

    private ArrayList<EntityBean> beanLists;

    private BaseApplication application;

    private View emptyIV;//无数据时的背景色

    private static final String PAGE = "10";

    public MyCollectView(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter parameter = new FormParameter();
        parameter.setShowtitle(false);
        return parameter;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_mycollect, null);
        if (mView == null) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        titleTV.setText("我的收藏");
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        emptyIV = mView.findViewById(R.id.empty_tv);
        mListView = (PullToRefreshListView) mView.findViewById(R.id.pulltorefresh_listview);
        mListView.setMode(PullToRefreshBase.Mode.BOTH);
        // 刚拉时的提示
        mListView.getLoadingLayoutProxy().setPullLabel("加载列表数据...");
        // 正在刷新提示
        mListView.getLoadingLayoutProxy().setRefreshingLabel("正在载入...");
        // 拉倒一定高度的提示
        mListView.getLoadingLayoutProxy().setReleaseLabel("放开以刷新...");
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (!isLoading) {
                    loadData(null);
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (!isLoading) {
                    if (beanLists == null || beanLists.size() < 1) {
                        loadData(null);
                        return;
                    }
                    EntityBean bean = beanLists.get(beanLists.size() - 1);
                    loadData(bean.getString("id"));
                }
            }
        });
        //设置适配器
        mAdapter = new MyCollectAdapter(mContext);
        mAdapter.setData(beanLists);
        mListView.setAdapter(mAdapter);
        //设置点击事件
        setListener(true);

        application = (BaseApplication) mContext.getApplicationContext();
        if (beanLists == null) {
            beanLists = new ArrayList<EntityBean>();
        } else {
            beanLists.clear();
        }
    }

    private void setListener(boolean flag) {
        if (null != mListView) {
            mListView.setOnItemClickListener(flag ? this : null);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position < 1) {
            return;
        }
        EntityBean bean = beanLists.get(position - 1);
        Intent cintent = new Intent(mContext, CarNewsActivity.class);
        cintent.putExtra("databean", bean);
        mContext.startActivity(cintent);
    }

    private void loadData(final String id) {
        isLoading = true;
        String username = application.getKckpName();
        String token = application.getToken();
        ApiImpI.getInstance(application).collectNewsList(username, token, id, PAGE,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        if (null != mListView) {// 停止刷新
                            mListView.onRefreshComplete();
                        }
                        DialogUtils.closeDialog();
                        isLoading = false;
                    }

                    @Override
                    public void onSuccess(Object result) {
                        if (null == mListView) {// 停止刷新
                            return;
                        }
                        mListView.onRefreshComplete();
                        DialogUtils.closeDialog();
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        if (TextUtils.equals("1", restate)) {
                            EntityBean[] datas = bean.getBeans("data");
                            if (TextUtils.isEmpty(id)) {
                                beanLists.clear();
                            }
                            if (null != datas) {
                                for (EntityBean data : datas) {
                                    beanLists.add(data);
                                }
                            }
                        }
                        if (null == beanLists || beanLists.size() <= 0) {
                            mListView.setEmptyView(emptyIV);
                        } else {
                            emptyIV.setVisibility(View.GONE);//null
                        }
                        if (null != mAdapter) {
                            mAdapter.setData(beanLists);
                            mAdapter.notifyDataSetChanged();
                        }
                        isLoading = false;
                    }
                });
    }

    @Override
    public void refresh() {
        if (!isLoading) {
            DialogUtils.showDialog(mContext);
            loadData(null);
        }
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        setListener(false);
        if (null != mListView) {
            mListView.setAdapter(null);
        }
        mListView = null;
        mAdapter = null;
        if (null != beanLists) {
            beanLists.clear();
            beanLists = null;
        }
        mView = null;
        DialogUtils.destroyDialog();
    }
}


